package com.youlu.campus.service.ca.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.UserParticipateActivityInfo;
import com.youlu.campus.entity.UserRecieveCaOpt;
import com.youlu.campus.entity.VO.req.ReceiveCaVO;
import com.youlu.campus.entity.WechatH5UserPO;
import com.youlu.campus.entity.enums.TaskBizType;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.ca.CaGenerateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
public class CaGenerateServiceImpl implements CaGenerateService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ActivityService activityService;

    @Override
    @Async
    public CompletableFuture<String> generateOne(UserParticipateActivityInfo up, String appId, String activityId) {
        UserPO userPO = null;
        log.info(":>>> 平台:wechat-h5");
        Query queryUser = new Query().addCriteria(Criteria.where("accountInfoId").is(up.getUserId()));
        if (StringUtils.isNotBlank(appId)) {
            queryUser.addCriteria(Criteria.where("appId").is(appId));
        }
        WechatH5UserPO wechatH5UserPO = mongoTemplate.findOne(queryUser, WechatH5UserPO.class);
        if (Objects.nonNull(wechatH5UserPO)) {
            log.error(":>>> 微信用户存在:{}", up.getUserId());
            userPO = new UserPO();
            BeanUtils.copyProperties(wechatH5UserPO, userPO);
            userPO.setId(up.getUserId());
        }
        if (Objects.isNull(userPO)) {
            log.error(":>>> 用户不存在:{}", up.getUserId());
            return CompletableFuture.completedFuture("OK");
        }
        ReceiveCaVO receiveCaVO = activityService.receiveCa(activityId, appId, userPO);

        if (Boolean.FALSE.equals(receiveCaVO)) {
            log.error(":>>> 运行错误:-----");
            return CompletableFuture.completedFuture("OK");
        }
        // 遍历纸质版证书
        for (String k : receiveCaVO.getPaperCas().keySet()) {
            log.info(":>>> 开始处理证书:{}", k);
            if (receiveCaVO.getPaperCas().containsKey(k)) {
//                    if (receiveCaVO.getPaperCas().containsKey(k) && !"暂无证书".equals(receiveCaVO.getPaperCas().get(k))) {
                UserRecieveCaOpt opt = new UserRecieveCaOpt();
                opt.setReceiveCaVO(receiveCaVO);
                opt.setActivityId(activityId);
                opt.setUserId(userPO.getId());
                switch (k) {
                    case "INVITE":
                        opt.setTaskBizType(TaskBizType.INVITE);
                        break;
                    case "QA":
                        opt.setTaskBizType(TaskBizType.QA);
                        break;
                    default:
                        break;
                }
                opt.setAppId(userPO.getAppId());
                opt.setOpenId(userPO.getOpenId());
                opt.setFrom("0");
                opt.setFromName("wechat-h5");
                opt.setStatus(0);
                opt.setCreatedTime(new Date());
                opt.setUniversityId(up.getUniversityId());
                opt.setUniversityName(up.getUniversityName());
                opt.setMobile(up.getMobile());
                opt.setTaskData(receiveCaVO.getDatas().get(TaskBizType.INVITE.name()));
                // 纸质版
                opt.setMaterial(1);
                opt.setCaGroupName(receiveCaVO.getPaperCas().get(k).getCaName());
                opt.setRecieveCaDetail(receiveCaVO.getPaperCas().get(k));
                opt.setReceiveCaVO(receiveCaVO);
                opt = mongoTemplate.insert(opt);
                log.info(":>>> 生成纸质版证书:{}", JSON.toJSONString(opt));
                // 电子版
                UserRecieveCaOpt optD = new UserRecieveCaOpt();
                optD.setActivityId(activityId);
                optD.setUserId(userPO.getId());
                switch (k) {
                    case "INVITE":
                        optD.setTaskBizType(TaskBizType.INVITE);
                        break;
                    case "QA":
                        optD.setTaskBizType(TaskBizType.QA);
                        break;
                    default:
                        break;
                }
                optD.setAppId(userPO.getAppId());
                optD.setOpenId(userPO.getOpenId());
                optD.setFrom("0");
                optD.setFromName("wechat-h5");
                optD.setStatus(0);
                optD.setCreatedTime(new Date());
                optD.setUniversityId(up.getUniversityId());
                optD.setUniversityName(up.getUniversityName());
                optD.setMobile(up.getMobile());
                optD.setTaskData(receiveCaVO.getDatas().get(TaskBizType.INVITE.name()));
                // 电子版
                optD.setMaterial(0);
                optD.setCaGroupName(receiveCaVO.getPaperCas().get(k).getCaName());
                optD.setRecieveCaDetail(receiveCaVO.getPaperCas().get(k));
                optD.setReceiveCaVO(receiveCaVO);
                optD = mongoTemplate.insert(optD);
                log.info(":>>> 生成电子版证书:{}", JSON.toJSONString(optD));
            }

        }
        return CompletableFuture.completedFuture("OK");
    }

}
